package com.dmall.algorithm.chapter02.other;

public class Leetcode24V1 {

    private static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }

    public ListNode swapPairs(ListNode head) {

        ListNode dummy = new ListNode(-1);
        dummy.next = head;

        ListNode prev = dummy;
        ListNode cur = head;
        while (cur != null && cur.next != null) {

            ListNode firstNode = cur;
            ListNode secondNode = cur.next;

            firstNode.next = secondNode.next;
            secondNode.next = firstNode;

            prev.next = secondNode;
            prev = firstNode;

            cur = firstNode.next;
        }
        return dummy.next;
    }
}
